package com.itheima.tcp.fileupload.demo2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class ServiceDemo {
    public static void main(String[] args) throws IOException {
        //创建服务器 Socket 对象 ， 监听 10000 端口
        ServerSocket serverSocket = new ServerSocket(10000);
        //监听客户端连接
        Socket accept = serverSocket.accept();
        //通过accept获取输入流对象
        InputStream inputStream = accept.getInputStream();
        //创建本地输出流对象，将读取的文件写入磁盘
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\filedemo\\input\\" + UUID.randomUUID().toString() + ".jpg"));

        byte[] bytes = new byte[1024];
        int len;
        while ((len = inputStream.read(bytes)) != -1){
            bos.write(bytes,0,len);
        }
        bos.close();

        //服务器反馈
        //创建网络输出流对象
        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("上传成功".getBytes());

        accept.shutdownOutput();

        accept.close();
        serverSocket.close();
    }
}
